package com.company.recruit.service.impl;

import com.company.recruit.dao.RecruitDao;
import com.company.recruit.entity.*;
import com.company.recruit.service.RecruitService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;


@Service("recruitService")
public class RecruitServiceImpl extends ServiceImpl<RecruitDao, RecruitEntity> implements RecruitService {

    @Override
    public List<EducationAnalyse> getEducationAnalyse() {
        List<EducationAnalyse> data = this.baseMapper.getEducationAnalyse();
        return data;
    }

    @Override
    public void saveEducationAnalyse(EducationAnalyse educationAnalyse) {
        Integer num = educationAnalyse.getNum();
        String education = educationAnalyse.getEducation();
        this.baseMapper.saveEducationAnalyse(num,education);
    }

    @Override
    public List<RecruitNum> getRecruitNum() {
        return this.baseMapper.getRecruitNum();
    }

    @Override
    public void saveRecruitNum(RecruitNum recruitNum) {
        String city = recruitNum.getCity();
        Integer num = recruitNum.getNum();
        this.baseMapper.saveRecruitNum(city,num);
    }

    @Override
    public List<RecruitNum> getCity(String name) {
        return this.baseMapper.getCity(name);
    }

    @Override
    public void deleteRecruitNum(String city) {
        this.baseMapper.deleteRecruitNum(city);
    }

    @Override
    public List<AreaAvgWage> getAreaAvgWage() {
        return this.baseMapper.getAreaAvgWage();
    }

    @Override
    public void saveAreaAvgWage(AreaAvgWage data) {
        this.baseMapper.saveAreaAvgWage(data);
    }

    @Override
    public List<AreaAvgWage> getCityByAvg(String city) {
        return this.baseMapper.getCityByAvg(city);
    }

    @Override
    public void deleteAreaAvgWage(String city) {
        this.baseMapper.deleteAreaAvgWage(city);
    }

    @Override
    public List<AreaAvgWage> getAll() {
        return this.baseMapper.getAll();
    }

    @Override
    public void saveAvg(String city, Integer avgMin, Integer avgMax) {
        this.baseMapper.saveAvg(city,avgMin,avgMax);
    }

    @Override
    public List<EducationAnalyse> getAllEducationAnalyse() {
        return this.baseMapper.getAllEducationAnalyse();
    }

    @Override
    public List<RecruitNum> getAllRecruitNum() {
        return this.baseMapper.getAllRecruitNum();
    }

    @Override
    public List<ExperienceTypeAnalyse> getAllExperienceTypeAnalyse() {
        return this.baseMapper.getAllExperienceTypeAnalyse();
    }

    @Override
    public void saveExperienceTypeAnalyse(List<ExperienceTypeAnalyse> data) {
        data.stream().forEach( (experienceType) ->{
            this.baseMapper.saveExperienceType(experienceType);
        });
    }

    @Override
    public List<ExperienceTypeAnalyse> getAllExperienceTypeAnalyseByClean() {
        return this.baseMapper.getAllExperienceTypeAnalyseByClean();
    }

    @Override
    public List<String> getStartWelfare() {
        return this.baseMapper.getStartWelfare();
    }

    @Override
    public List<WelfareAnalyse> getWelfareByAnalyse() {

        List<String> welfare = new ArrayList<>();

        welfare.add("专业培训");
        welfare.add("五险一金");
        welfare.add("交通补贴");
        welfare.add("周末双休");
        welfare.add("员工旅游");
        welfare.add("定期体检");
        welfare.add("带薪年假");
        welfare.add("年终奖");
        welfare.add("节日福利");
        welfare.add("零食下午茶");
        welfare.add("餐补");

        List<WelfareAnalyse> dataList = new ArrayList<>();

        welfare.stream().forEach( (data) ->{
            WelfareAnalyse welfareAnalyse = this.baseMapper.getWelfareAnalyse(data);

            if( !StringUtils.isEmpty(welfareAnalyse)) {
                this.baseMapper.saveWelfareAnalyse(data,welfareAnalyse.getNum());
            }

            dataList.add(welfareAnalyse);
        });


        return dataList;
    }

    @Override
    public List<ExperienceTypeAnalyse> getAllExperienceTypeAnalyseByList() {
        return this.baseMapper.getAllExperienceTypeAnalyseByList();
    }

    @Override
    public List<WelfareAnalyse> getWelfareByAnalyseByList() {
        return this.baseMapper.getWelfareByAnalyseByList();
    }

    @Override
    public void saveRecruitEntity(RecruitMongo recruitEntity) {
        this.baseMapper.saveRecruitEntity(recruitEntity);
    }

    @Override
    public void getRecruitNumByService() {
        // 获取统计信息
        List<RecruitNum> dataFromDb = this.getRecruitNum();

        List<String> listCity = new ArrayList<>();
        listCity.add("工程师");
        listCity.add("加班");

        dataFromDb.stream().forEach((data) -> {
            for (String s : listCity) {
                if(data.getCity().contains(s)) {
                    this.baseMapper.deleteRecruitNumByNew(s);
                }
            }
        });

        List<RecruitNum> recruitNumNew = this.getRecruitNum();

        // 保存数据
        recruitNumNew.stream().forEach((data) -> {
            this.saveRecruitNum(data);
        });

        // 保留其他字段
        List<String> cityString = new ArrayList<>();
        cityString.add("北京");
        cityString.add("上海");
        cityString.add("广州");
        cityString.add("深圳");
        cityString.add("成都");
        cityString.add("杭州");
        cityString.add("天津");
        cityString.add("西安");
        cityString.add("苏州");
        cityString.add("重庆");
        cityString.add("武汉");
        cityString.add("厦门");
        cityString.add("长沙");
        cityString.add("郑州");

        Integer sum = 0;

        for (String city : cityString) {
            RecruitNum recruitNum = new RecruitNum();

            List<RecruitNum> data = this.getCity(city);

            for (RecruitNum datum : data) {
                Integer num = datum.getNum();
                sum = sum + num;
            }

            recruitNum.setCity(city);
            recruitNum.setNum(sum);

            // 删除
            this.deleteRecruitNum(city);

            // 插入
            this.saveRecruitNum(recruitNum);



            sum = 0;
        }


        // 过滤错误字段
        List<AreaAvgWage> listFromDb = this.baseMapper.getAllAvgByNew();
        listFromDb.stream().forEach( (data) ->{
            if(data.getCity().length() > 5 || data.getCity().contains(")")) {
                // 删除
                this.baseMapper.deleteAvg(data.getCity());
            }
        });






    }

    @Override
    public void getAreaAvgWageByService() {
        // 获取所有数据
        List<AreaAvgWage> dataFromDb = this.getAreaAvgWage();

        // 保存平均工资
        dataFromDb.stream().forEach((data) -> {
                this.saveAreaAvgWage(data);
        });

        // 过滤重复字段
        List<String> cityString = new ArrayList<>();
        cityString.add("北京");
        cityString.add("上海");
        cityString.add("广州");
        cityString.add("深圳");
        cityString.add("成都");
        cityString.add("杭州");
        cityString.add("天津");
        cityString.add("西安");
        cityString.add("苏州");
        cityString.add("重庆");
        cityString.add("武汉");
        cityString.add("厦门");
        cityString.add("长沙");
        cityString.add("郑州");

        // 最小总工资
        Integer sumMinWage = 0;
        // 最大总工资
        Integer sumMaxWage = 0;
        // 数量
        Integer countNum = 0;

        for (String city : cityString) {
            AreaAvgWage areaAvgWage = new AreaAvgWage();


            List<AreaAvgWage> data = this.getCityByAvg(city);


            for (AreaAvgWage datum : data) {

                // 最小工资总和
                Integer sumMinWageFromDb = datum.getSumMinWage();
                sumMinWage = sumMinWage + sumMinWageFromDb;

                // 最大工资总和
                Integer sumMaxWageFromDb = datum.getSumMaxWage();
                sumMaxWage = sumMaxWage + sumMaxWageFromDb;

                // 数量
                Integer countNumFromDb = datum.getCountNum();
                countNum = countNum + countNumFromDb;

            }

            areaAvgWage.setCity(city);
            areaAvgWage.setSumMinWage(sumMinWage);
            areaAvgWage.setSumMaxWage(sumMaxWage);
            areaAvgWage.setCountNum(countNum);

            // 删除
            this.deleteAreaAvgWage(city);

            // 插入
            this.saveAreaAvgWage(areaAvgWage);



            sumMinWage = 0;
            sumMaxWage = 0;
            countNum = 0;
        }


        // 求平均工资
        List<AreaAvgWage> dataFromDbToAvg = this.getAll();
        dataFromDbToAvg.stream().forEach( (areaAvgWage) ->{

            String city = areaAvgWage.getCity();

            Integer avgMin = 0;
            Integer avgMax = 0;

            Integer sumMinWageToAvg = areaAvgWage.getSumMinWage();
            Integer sumMaxWageToAvg = areaAvgWage.getSumMaxWage();
            Integer countNumToAvg = areaAvgWage.getCountNum();

            avgMin = sumMinWageToAvg / countNumToAvg;
            avgMax = sumMaxWageToAvg / countNumToAvg;

            this.saveAvg(city,avgMin,avgMax);

        });


        // 过滤错误字段
        List<AreaAvgWage> listFromDb = this.baseMapper.getAllAvgByNew();
        listFromDb.stream().forEach( (data) ->{
            if(data.getCity().length() > 5 || data.getCity().contains(")")) {
                // 删除
                this.baseMapper.deleteAvg(data.getCity());
            }
        });


    }



}